-- SPDX-License-Identifier: GPL-3.0-or-later

local extension = Package:new("guance1")
-- extension.extensionName = "vupslash"
extension.extensionName = "VslTest"
-- extension.game_modes_whitelist = { 'nos_heg_mode', 'new_heg_mode' }
-- dofile "packages/vupslash/game_rule.lua"

-- local VslUtil = require "packages/vupslash/util"
local VslUtil = require "packages/VslTest/util"

--------------------------------------------------
--测试技能：超级英姿
--------------------------------------------------

local super_yingzi = fk.CreateTriggerSkill{
  name = "superyingzi",
  anim_type = "drawcard",
  events = {fk.DrawNCards},
  on_use = function(self, event, target, player, data)
    data.n = data.n + 20
  end,
}

--------------------------------------------------
--测试技能：摸指定牌
--------------------------------------------------

local v_cheat = fk.CreateActiveSkill{
  name = "v_cheat",
  anim_type = "drawcard",
  can_use = function(self, player)
      return true
  end,
  on_use = function(self, room, effect)
      local from = room:getPlayerById(effect.from)
      local cardTypeName = room:askForChoice(from, { 'basic', 'trick', 'equip' }, "cheat")
      local cardType = Card.TypeBasic
      if cardTypeName == 'trick' then
          cardType = Card.TypeTrick
      elseif cardTypeName == 'equip' then
          cardType = Card.TypeEquip
      end

      local allCardIds = Fk:getAllCardIds()
      local allCardMapper = {}
      local allCardNames = {}
      for _, id in ipairs(allCardIds) do
          local card = Fk:getCardById(id)
          if card.type == cardType then
              if allCardMapper[card.name] == nil then
                  table.insert(allCardNames, card.name)
              end

              allCardMapper[card.name] = allCardMapper[card.name] or {}
              table.insert(allCardMapper[card.name], id)
          end
      end

      if #allCardNames == 0 then
          return
      end

      local cardName = room:askForChoice(from, allCardNames, "cheat")
      local toGain = nil
      if #allCardMapper[cardName] > 0 then
          toGain = allCardMapper[cardName][math.random(1, #allCardMapper[cardName])]
      end

      from:addToPile(self.name, toGain, true, self.name)
      room:obtainCard(effect.from, toGain, true, fk.ReasonPrey)
  end
}

--------------------------------------------------
--测试技能：流失体力
--------------------------------------------------

local v_lose = fk.CreateActiveSkill{
  name = "v_lose",
  anim_type = "offensive",
  can_use = function(self, player)
      return true
  end,
  target_num = 1,
  -- card_num = 1,
  card_filter = function(self, to_select, selected, selected_targets)
      return false
      -- if #selected == 1 then return false end
      -- return Fk:currentRoom():getCardArea(to_select) ~= Player.Equip
  end,
  target_filter = function(self, to_select, selected)
      -- local target = Fk:currentRoom():getPlayerById(to_select)
      return #selected == 0
  end,
  on_use = function(self, room, effect)
      local player = room:getPlayerById(effect.from)
      local target = room:getPlayerById(effect.tos[1])
      room:loseHp(target, 1, self.name)
  end,
}

--------------------------------------------------
--轮转
--+2可用表情：挑衅（咕唧挑衅，六青屑，lida剪刀，喵纸得意，喵纸擦汗）
---3表情方向：伤心（鹿鸣哭，咕唧网络不好，TSU网络不好，六青哭，lida哭，lida害怕）
--撞上插结（放动画）会直接完蛋，官服建议先关了动画。
--------------------------------------------------

local v_lunzhuan_fury = fk.CreateTriggerSkill{
    name = "#v_lunzhuan_fury",
    --赋予特殊型技能定义
    anim_type = "special",
    --技能为锁定技，满足条件后强制发动
    frequency = Skill.Compulsory,
    -- mute = true,
    --时机：阶段开始时，轮次开始时，额定摸牌时，指定目标时
    events = {fk.RoundStart},
    --触发条件（已完成）：
    --  触发时机的角色为遍历到的角色。
    --  轮次开始时：
    --    遍历到的角色存在此技能；
    can_trigger = function(self, event, target, player, data)
      if not target == player then return false end
      return player:hasSkill(self)
    end,
    --使用效果（基本完成）：
    --  轮次开始时：
    --    根据当前轮次为player增加天怒标记；
    on_use = function(self, event, target, player, data)
      local room = player.room
      room:addPlayerMark(player, "@v_tiannu", room:getTag("RoundCount"))
    end,
  }
  local v_lunzhuan = fk.CreateTriggerSkill{
    --（刚需）技能认证名
    name = "v_lunzhuan",
    --(非必要）赋予特殊型技能定义
    anim_type = "special",
    --技能为锁定技，满足条件后强制发动
    frequency = Skill.Compulsory,
    --启动后不默认播放技能触发
    mute = true,
    --时机：受到伤害后，造成伤害后
    events = {fk.Damaged,fk.Damage},
    --触发条件：
    --触发时机的角色为遍历到的角色、遍历到的角色具有本技能、造成的伤害大于0。
    --···
    can_trigger = function(self, event, target, player, data)
      if not (target == player and player:hasSkill(self) and
        data.damage > 0) then return false end
      if event == fk.Damaged then
        local room = player.room
        return 
          room:getTag("RoundCount") >= 1 and
          data.from and data.from:isAlive() and data.from ~= player
      elseif event == fk.Damage then
        return data.to 
          -- and data.to:isAlive() 
          and data.to ~= player
      end
      return true
    end,
    -- on_cost = function(self, event, target, player, data)
    --   local room = player.room
    --   local prompt = nil
    --   if event == fk.DamageInflicted then
    --     prompt = "v_lunzhuan_damaged_choice::"..player.id
    --     if room:askForSkillInvoke(data.from, self.name, data, prompt) then
    --       return true
    --     end
    --   elseif event == fk.DamageCaused then
    --     prompt = "v_lunzhuan_damage_choice::"..data.to.id
    --     if room:askForSkillInvoke(player, self.name, data, prompt) then
    --       return true
    --     end
    --   end
    -- end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      if event == fk.Damaged then
        -- room:notifySkillInvoked(data.from, self.name)
        room:notifySkillInvoked(player, self.name)
        player:broadcastSkillInvoke(self.name, -1)
        local roundcount = room:getTag("RoundCount")
        if roundcount - 3 < 0 then roundcount = 0
        elseif roundcount - 3 >= 0 then roundcount = roundcount - 3 end
        room:setTag("RoundCount", roundcount)
        local pls = room.players
        --为全体玩家更新轮数
        for _,p in ipairs(pls) do
          p:doNotify("UpdateRoundNum", roundcount or 0)
        end
        room:sendLog{
          type = "#v_minusround",
          arg = self.name,
          arg2 = room:getTag("RoundCount"),
        }
      elseif event == fk.Damage then
        room:notifySkillInvoked(player, self.name)
        player:broadcastSkillInvoke(self.name, -1)
        room:setTag("RoundCount", room:getTag("RoundCount") + 2)
        local pls = room.players
        --为全体玩家更新轮数
        for _,p in ipairs(pls) do
          p:doNotify("UpdateRoundNum", room:getTag("RoundCount") or 0)
        end
        room:sendLog{
          type = "#v_addround",
          arg = self.name,
          arg2 = room:getTag("RoundCount"),
        }
      end
      -- return true
    end,
  }
  
  v_lunzhuan:addRelatedSkill(v_lunzhuan_fury)
    
  --------------------------------------------------
  --不息
  --------------------------------------------------
  
  -- local v_buxi_buff = fk.CreateMaxCardsSkill{
  --   name = "#v_buxi_buff",
  --   correct_func = function(self, player)
  -- 		local X = player:getMark("@v_tiannu")
  --     if player:hasSkill(self) and X > 0 then
  --       return math.ceil(X / 4.0)
  --     end
  --   end,
  -- }
  -- local v_buxi = fk.CreateTriggerSkill{
  --   name = "v_buxi",
  --   --赋予特殊型技能定义
  --   anim_type = "special",
  --   --技能为锁定技，满足条件后强制发动
  --   frequency = Skill.Compulsory,
  --   -- mute = true,
  --   --时机：阶段开始时，轮次开始时，额定摸牌时，指定目标时
  --   events = {fk.RoundStart},
  --   --触发条件（已完成）：
  --   --  触发时机的角色为遍历到的角色。
  --   --  轮次开始时：
  --   --    遍历到的角色存在此技能；
  --   can_trigger = function(self, event, target, player, data)
  --     if not target == player then return false end
  --     return player:hasSkill(self)
  --   end,
  --   --使用效果（基本完成）：
  --   --  轮次开始时：
  --   --    根据当前轮次为player增加天怒标记；
  --   on_use = function(self, event, target, player, data)
  --     local room = player.room
  --     room:addPlayerMark(player, "@v_tiannu", room:getTag("RoundCount"))
  --   end,
  -- }
  
  -- v_buxi:addRelatedSkill(v_buxi_buff)
  
  --------------------------------------------------
  --天怒
  --------------------------------------------------
    
  local v_tiannu_cardbuff = fk.CreateMaxCardsSkill{
    name = "#v_tiannu_cardbuff",
    correct_func = function(self, player)
          local X = player:getMark("@v_tiannu")
      if player:hasSkill(self) and X > 0 then
        return math.ceil(X / 4.0)
      end
    end,
  }
  local v_tiannu_buff = fk.CreateTargetModSkill{
    name = "#v_tiannu_buff",
    --存在天怒额外出杀标记
    residue_func = function(self, player, skill, scope, card)
      if player:getMark("@v_tiannu_time-turn") > 0
        and skill.trueName == "slash_skill" and scope == Player.HistoryPhase then
        return player:getMark("@v_tiannu_time-turn")
      end
    end,
    --存在天怒额外目标标记
    --铁锁的问题和天怒无关（乐）
    extra_target_func = function(self, player, skill, card)
      if player:getMark("@v_tiannu_target-turn") > 0 and skill.trueName == "slash_skill" then
        -- local cards = card:isVirtual() and card.subcards or {card.id}
        return player:getMark("@v_tiannu_target-turn")
      end
    end,
  }
  local v_tiannu = fk.CreateTriggerSkill{
    name = "v_tiannu",
    --赋予进攻型技能定义
    anim_type = "offensive",
    mute = true,
    --时机：阶段开始时，额定摸牌时
    events = {fk.EventPhaseStart, fk.DrawNCards},
    --触发条件（已完成）：
    --  触发时机的角色为遍历到的角色。
    --  阶段开始时：
    --    遍历到的角色存在此技能；遍历到的角色处于准备阶段；存在天怒标记
    --  额定摸牌时：
    --    存在天怒摸牌标记
    --  ···
    can_trigger = function(self, event, target, player, data)
      if not target == player then return false end
      if event == fk.EventPhaseStart then
        return target == player and player:hasSkill(self) 
          and player.phase == Player.Start
          and player:getMark("@v_tiannu") >= 4
      --满足技能发动要求后，锁定发动。
      elseif event == fk.DrawNCards then
        return target == player and player:getMark("v_tiannu_draw-turn") > 0
      end
    end,
    --使用条件（初步完成）：
    --  阶段开始时：
    --    询问是否发动，如果是，则发动
    --  轮次开始时：
    --    锁定发动；
    --  额定摸牌时：
    --    锁定发动；
    on_cost = function(self, event, target, player, data)
      --确认是否发动技能。
      if event == fk.EventPhaseStart then
        local room = player.room
        --拼提示字符串
        --目前prompt不支持arg3，血崩。
        local X = player:getMark("@v_tiannu")
        local choice = function (X)
          if X >= 12 then 
            -- return {3, math.ceil(X / 5.0), math.ceil(X / 8.0), math.ceil(X / 40.0)}
            return {3, math.ceil(X / 5.0), math.ceil(X / 8.0), nil}
          elseif X >= 8 then return {2, math.ceil(X / 5.0), math.ceil(X / 8.0), nil} end
          return {1, math.ceil(X / 5.0), nil, nil}
        end
        local prompt = "v_tiannu_choice_" .. choice(X)[1] .. "::"
        local times = choice(X)[1]
        if times > 2 then times = 2 end
        for i = 1, times do
          -- local num = function (i)
          --   if i ~= 1 then return i end
          --   return 1
          -- end
          local ar = ":" .. choice(X)[i+1]
          -- if num(i) > 1 then ar = ar .. num(i) end
          prompt = prompt .. ar
        end
        if room:askForSkillInvoke(player, self.name, data, prompt) then
          return true
        end
      --满足技能发动要求后，锁定发动。
      else
        return true
      end
    end,
    --使用效果（基本完成）：
    --  阶段开始时：
    --    归零天怒标记；根据三种情况获取三种天怒标记（可以以后考虑在这里加个log）
    --    根据标记数量放表情包（>=12：kaya拔刀，lida路由器来咯）
    --  轮次开始时：
    --    根据当前轮次为player增加天怒标记；
    --  额定摸牌时：
    --    根据标记数增加额定摸牌数；
    on_use = function(self, event, target, player, data)
      local room = player.room
      if event == fk.EventPhaseStart then
        -- room:broadcastSkillInvoke(self.name, -1)
        player:broadcastSkillInvoke(self.name)
        room:notifySkillInvoked(player, self.name)
        -- room:delay(2700)
        local X = player:getMark("@v_tiannu")
        room:setPlayerMark(player, "@v_tiannu", 0)
        -- 更换标签
        if X >= 12 then room:setPlayerMark(player, "@v_tiannu_target-turn", math.ceil(X / 40.0)) end
        if X >= 8 then room:setPlayerMark(player, "@v_tiannu_time-turn", math.ceil(X / 8.0)) end
        if X >= 4 then room:setPlayerMark(player, "v_tiannu_draw-turn", math.ceil(X / 5.0)) end
        -- log
        if X >= 12 then 
          room:sendLog{
            type = "#v_tiannu_log3",
            from = player.id,
            arg = player:getMark("v_tiannu_draw-turn"),
            arg2 = player:getMark("@v_tiannu_time-turn"),
            arg3 = player:getMark("@v_tiannu_target-turn"),
          } 
        elseif X >= 8 then 
          room:sendLog{
            type = "#v_tiannu_log2",
            from = player.id,
            arg = player:getMark("v_tiannu_draw-turn"),
            arg2 = player:getMark("@v_tiannu_time-turn"),
          } 
        elseif X >= 4 then 
          room:sendLog{
            type = "#v_tiannu_log1",
            from = player.id,
            arg = player:getMark("v_tiannu_draw-turn"),
          } 
        end
      elseif event == fk.DrawNCards then
        data.n = data.n + player:getMark("v_tiannu_draw-turn")
      end
    end,
  
    --出杀次数在这里刷新（等“出牌阶段空闲时间点开始时”时机出来再说；杀的现有次数之类的参考杀这张牌）。
    -- refresh_events = {fk.AfterCardUseDeclared},
    -- 刷新时机：
    --             触发时机的角色为遍历到的角色；遍历到的角色具有本技能；
    --             存在回合开始阶段时使用此技能的标签；
    --             造成的伤害大于0；
    --             非光属性伤害（暂未实装）。
    -- can_refresh = function(self, event, target, player, data)
    --   if not (target == player and player:hasSkill(self)) then
    --     return false
    --   end
    --   -- if event == fk.DamageCaused then
    --   return player:getMark("v_tiannu_time-turn") > 0
    --     and data.damage > 0
    --   -- elseif event == fk.Damage then
    --   --   return player:getMark("v_motiao_using-turn") > 0
    --   -- end
    -- end,
    -- on_refresh = function(self, event, target, player, data)
    --   local room = player.room
    --   if event == fk.DamageCaused then
    --     local x = math.min(data.damage, 1 - player:getMark("#turn_damage-turn"))
    --     if x < data.damage then
    --       if x > 0 then
    --         data.damage = 1
    --         --return false
    --       else
    --         room:setEmotion(data.to, "./packages/vupslash/image/anim/skill_nullify")
    --         room:sendLog{
    --           type = "#defense_damage",
    --           from = player.id,
    --           --log.to是数组
    --           to = {data.to.id},
    --           arg = self.name,
    --           arg2 = data.damage,
    --         }
    --         data.damage = 0
    --       end
    --     end
    --   elseif event == fk.Damage then
    --     room:setPlayerMark(player, "@@v_motiao_using-turn", 0)
    --     room:setPlayerMark(player, "@@v_motiao_after-turn", 1)
    --   end
    -- end
  }
  -- local v_tiannu = fk.CreateTriggerSkill{
  --   name = "v_tiannu",
  --   --赋予进攻型技能定义
  --   anim_type = "offensive",
  --   --技能为锁定技，满足条件后强制发动
  --   frequency = Skill.Compulsory,
  --   mute = true,
  --   --时机：阶段开始时，轮次开始时，额定摸牌时，指定目标时
  --   events = {fk.RoundStart},
  --   --触发条件（已完成）：
  --   --  触发时机的角色为遍历到的角色。
  --   --  轮次开始时：
  --   --    遍历到的角色存在此技能；
  --   can_trigger = function(self, event, target, player, data)
  --     if not target == player then return false end
  --     return player:hasSkill(self)
  --   end,
  --   --使用效果（基本完成）：
  --   --  轮次开始时：
  --   --    根据当前轮次为player增加天怒标记；
  --   on_use = function(self, event, target, player, data)
  --     local room = player.room
  --     room:addPlayerMark(player, "@v_tiannu", room:getTag("RoundCount"))
  --   end,
  -- }
  
  v_tiannu:addRelatedSkill(v_tiannu_buff)
  v_tiannu:addRelatedSkill(v_tiannu_cardbuff)
    
  --------------------------------------------------
  --映光AR5840
  --------------------------------------------------
    
  local yingguangAR5840_wujiujiusi = General(extension, "yingguangAR5840_wujiujiusi", "guancezhan", 3, 3, General.Agender)
  yingguangAR5840_wujiujiusi:addSkill(v_lunzhuan)
  -- yingguangAR5840_wujiujiusi:addSkill(v_buxi)
  yingguangAR5840_wujiujiusi:addSkill(v_tiannu)
  -- yingguangAR5840_wujiujiusi:addSkill(v_cheat)

  -- 加载本包的翻译包(load translations of this package)，这一步在本文档的最后进行。
-- dofile "packages/vupslash/i18n/init.lua"
dofile "packages/VslTest/i18n/init.lua"
  
-- return { extension } 

return extension